package com.agileai.bpm.controller.emulation;

import java.util.List;

import com.agileai.bpm.bizmoduler.emulation.WfEmulationManage;
import com.agileai.bpm.domain.define.Activity;
import com.agileai.bpm.domain.define.Process;
import com.agileai.bpm.domain.running.ProcessInstance;
import com.agileai.bpm.service.running.ProcessHandle;
import com.agileai.bpm.service.running.RunningException;
import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.controller.core.BaseHandler;
import com.agileai.hotweb.domain.FormSelect;
import com.agileai.hotweb.domain.FormSelectFactory;
import com.agileai.hotweb.renders.LocalRenderer;
import com.agileai.hotweb.renders.RedirectRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.util.ListUtil;
import com.agileai.util.StringUtil;

public class WfEmulationManageDetailHandler extends BaseHandler{
	public WfEmulationManageDetailHandler(){
		super();
		this.serviceId = buildServiceId(WfEmulationManage.class);
	}
	public ViewRenderer doBackAction(DataParam param){
		return new RedirectRenderer(getHandlerURL(WfEmulationManageListHandler.class));
	}
	public ViewRenderer prepareDisplay(DataParam param) {
		String processId = param.get("WFP_ID");
		String bizRecordId = param.get("bizRecordId");
		String activityCode = param.get("WFA_CODE");
		ProcessHandle processHandle = (ProcessHandle)this.lookupService("processHandle");
		try {
			Process process = processHandle.retrieveProcess(processId);
			this.setAttribute("WFP_NAME", process.getName());
			if (StringUtil.isNotNullNotEmpty(activityCode)){
				Activity activity = process.getActivity(activityCode);
				this.setAttribute("WFA_NAME", activity.getName());				
			}
			
			String isEmbed = param.getString("isEmbed", "false");
			this.setAttribute("isEmbed", isEmbed);
			WfEmulationManage wfEmulationManage = getService();
			DataParam queryParam = new DataParam();
			queryParam.put("WFE_ID",bizRecordId);
			DataRow dataRow = wfEmulationManage.getRecord(queryParam);
			if (dataRow != null){
				this.setAttributes(dataRow);
				String bizDataXML = dataRow.getString("WFE_CONTENT");
				if (bizDataXML != null){
					List<DataRow> bizDataSet =  ListUtil.toDataSet(bizDataXML);
					this.setAttribute("bizDataSet", bizDataSet);
				}			
			}else{
				String processInstId = param.get("WFIP_ID");
				ProcessInstance processInstance = processHandle.retrieveProcessInstance(processInstId);
				this.setAttribute("WFE_CREATERNAME", processInstance.getLaucherName());
				this.setAttribute("WFE_CREATETIME", processInstance.getLaucheTime());
				this.setAttribute("WFE_TITLE", processInstance.getTitle());
			}
		} catch (RunningException e) {
			e.printStackTrace();
			log.error(e.getLocalizedMessage());
		}
		FormSelect switchSelect = FormSelectFactory.createSwitchFormSelect();
		switchSelect.setHasBlankValue(false);
		setAttribute("varFormSelect", switchSelect);
		return new LocalRenderer(getPage());
	}	
	protected WfEmulationManage getService() {
		return (WfEmulationManage)this.lookupService(this.getServiceId());
	}
}
